package com.wyhello.tube4.dao.impl;

import java.math.BigInteger;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.wyhello.tube4.dao.ResideRecordsDAO;
import com.wyhello.tube4.pojo.Reside;

public class ResideRecordsDAOImpl  implements  ResideRecordsDAO{
	private SessionFactory sessionFactory;
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	/**
	 * 归属信息的录入
	 * @param  reside 要录入的信息
	 * @return 返回是否录入成功
	 */
	@Override
	public boolean saveReside(Reside  reside) {
		Session session = sessionFactory.getCurrentSession();
		int i = 1;
		try{
			session.save(reside);
		}catch(Exception e){
			i=0;
		}
		if(i==1){
			return true;
		}else{
			return false;
		}
	}
	/**
	 * 对归属信息进行批量删除
	 * @param ids 要删除的id
	 * @return 返回删除的条数
	 */
	@Override
	public int deleteReside(String ids) {
		Session session  =  sessionFactory.getCurrentSession();
		String sql = "delete from reside where id in "+ids;
		int i = session.createSQLQuery(sql).executeUpdate();
		return i;
	}
	/**
	 * 查询所有的归属信息
	 * @return 返回所有归属信息
	 */
	@Override
	public List<Reside> queryReside() {
		Session session  =  sessionFactory.getCurrentSession();
		String hql = "from Reside";
		List<Reside> list = session.createQuery(hql).list();
		return list;
	}
	/**
	 * 根据id查询归属信息
	 * @param id 要查询的id
	 * @return 为ID的归属信息
	 */
	@Override
	public Reside getResideById(int id) {
		Session session  =  sessionFactory.getCurrentSession();
		Reside reside = (Reside) session.get(Reside.class, Integer.valueOf(id));
		return reside;
	}
	/**
	 * 对归属信息进行修改
	 * @param change 修改后的信息
	 * @return 修改是否成功
	 */
	@Override
	public boolean updateReside(Reside reside) {
		Session session  =  sessionFactory.getCurrentSession();
		try{
			session.update(reside);
		}catch(Exception e){
			return false;
		}
		return true;
	}
	/**
	 * 查询所有的锅炉
	 * @return
	 */
	public List<Reside> queryResideByBoiler(){
		Session session  =  sessionFactory.getCurrentSession();
		List<Reside> resides = session.createQuery("from Reside where level=1").list();
		return resides;
		
	}
	/**
	 * 根据级别查询信息
	 * @param level 级别
	 * @return 信息
	 */
	public List<Reside> queryResideByLevel(int level){
		Session session  =  sessionFactory.getCurrentSession();
		List<Reside> resides = session.createQuery("from Reside where level="+level).list();
		return resides;
	}
	public void deleteOj(Reside reside){
		Session session  =  sessionFactory.getCurrentSession();
		session.delete(reside);
	}
	@Override
	public List<Reside> queryResidesByParendID(int id) {
		Session session  =  sessionFactory.getCurrentSession();
		List<Reside> resides =session.createQuery("from Reside where parentId="+id).list();
		return resides;
	}
	
	
	@Override
	public int queryCountOfChildrenByParentId(int parentId) {
		Session session  =  sessionFactory.getCurrentSession();
		List<BigInteger> l = session.createSQLQuery("SELECT COUNT(*) FROM Reside WHERE parentId = "+parentId).list();
		return l.get(0).intValue();
	}
}
